package org.jeecg.modules.base.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.base.mapper.VersionMapper;
import org.jeecg.modules.base.service.IVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class VersionServiceImpl implements IVersionService {

    @Autowired
    private VersionMapper versionMapper;

    @Override
    public Result checkVersion(String version, String platform) {
        // 初始化返回
        Result<Map<String,Object>> result = new Result<>();
        Map<String,Object> retMap =  new HashMap<>();
        try {

            //获取最新的版本信息
            Map<String,Object> latestMap = versionMapper.checkVersion(platform);
            //判读当前版本是否为最新版本
            if(null != latestMap){
                if(latestMap.get("version").equals(version)){
                    retMap.put("needToUpdate","0");
                }else{
                    retMap.put("needToUpdate","1");
                    retMap.put("latestVersion",latestMap.get("version"));
                    retMap.put("updateType",latestMap.get("updateType"));
                    retMap.put("updateUrl",latestMap.get("updateUrl"));
                    retMap.put("desc",latestMap.get("desc"));

                }
            }else{
                retMap.put("needToUpdate","0");
            }
            result.setResult(retMap);
            result.setCode(100);
            result.setSuccess(true);
            result.setMessage("获取app更新信息成功");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            result.setCode(500);
            result.setMessage("系统开小差了");
            result.setSuccess(false);
        }

        return result;
    }


}
